package com.neuedu.yyzx.controller;


import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neuedu.yyzx.common.CommonResult;
import com.neuedu.yyzx.entity.Permission;
import com.neuedu.yyzx.service.IPermissionService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * 权限 前端控制器
 * </p>
 *http://127.0.0.1:8080/permission/getByPaerent
 * @author dya
 * @since 2021-05-12
 */
@RestController
@RequestMapping("//permission")
public class PermissionController {

    @Resource
    IPermissionService permissionService;

    /**
     *
     * @param permission
     * @return
     */

    @RequestMapping("/list")
    CommonResult list(Permission permission){
        return CommonResult.success(permissionService.list(permission));
    }


    @RequestMapping("/add")
    CommonResult add(Permission permission){
        return CommonResult.success(permissionService.saveOrUpdate(permission));
    }

    @RequestMapping("/update")
    CommonResult update(Permission permission){
        return  CommonResult.success(permissionService.updateAll(permission));
    }

    @RequestMapping("/getById")
    CommonResult getById(Integer id){
        return CommonResult.success(permissionService.getById(id));
    }

    @RequestMapping("/del")
    CommonResult del(Integer id) {
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.set("active",0);
        updateWrapper.eq("id",id);
        return CommonResult.success(permissionService.update(updateWrapper));
    }
    @RequestMapping("/back")
    CommonResult back(Integer id) {
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.set("active",1);
        updateWrapper.eq("id",id);
        return CommonResult.success(permissionService.update(updateWrapper));
    }


    @RequestMapping("/userPermissionList")
    CommonResult userPermissionList(HttpServletRequest request) {

        String token = request.getHeader("token");

        Integer userId = JWT.decode(token).getClaim("id").asInt();
        String username = JWT.decode(token).getClaim("username").asString();


        return CommonResult.success(permissionService.userPermissionList(userId));
    }


}
